package com.xiaojimao.Utils;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.awt.image.BufferedImage;

@Component
@ConfigurationProperties(prefix = "orc")
public class OCRUtil {

    @Value("${orc.tempFileDir}")
    private String tempFileNameDir;

    @Value("${orc.dataPath}")
    private String dataPath;

    @Value("${orc.dataLanguage}")
    private String dataLanguage;

    public String scanImage(BufferedImage file) {
        try {
            ITesseract ocr = new Tesseract();
            // 设置字库数据源
            ocr.setDatapath(dataPath);
            // 设置数据源语言
            ocr.setLanguage(dataLanguage);
            // 扫描
            String info = ocr.doOCR(file);
            // 将结果格式化
            info = info.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
            // 返回
            return info;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
